package com.jsmtr.rms.service.impl;

import com.jsmtr.rms.dao.DaoService;
import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.IResumeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class ResumeService implements IResumeService {

    @Resource
    private DaoService dao;

    public void setDao(DaoService dao) {
        this.dao = dao;
    }

    //简历基本信息
    @Override
    public UserResume getUserResume(String userid) throws Exception {
        return (UserResume) dao.findUnique("from UserResume where userid='" + userid + "'");
    }

    //教育背景
    @SuppressWarnings("unchecked")
    @Override
    public List<UserEducation> getUserEducation(String rid) throws Exception {
        return dao.find("from UserEducation where rid='" + rid + "' order by educationcode desc");
    }

    //教育背景
    @SuppressWarnings("unchecked")
    @Override
    public List<UserEducation> getUserEducationByOrder(String rid, Map<String, String> orders) throws Exception {
        String hql="from UserEducation where rid='" + rid + "' ";
        if (orders!=null&&orders.size()>0){
            hql+= " order by ";
            for (String colunm : orders.keySet()){
                hql+=colunm+" "+orders.get(colunm)+",";
            }
            hql=hql.substring(0,hql.length()-1);
        }
        return dao.find(hql);
    }

    //工作经验
    @SuppressWarnings("unchecked")
    @Override
    public List<UserWorkExperience> getUserWorkExperience(String rid) throws Exception {
        return dao.find("from UserWorkExperience where rid='" + rid + "'");
    }

    //培训经历
    @SuppressWarnings("unchecked")
    @Override
    public List<UserTraining> getUserTraining(String rid) throws Exception {
        return dao.find("from UserTraining where rid='" + rid + "'");
    }


    //获奖信息
    @SuppressWarnings("unchecked")
    @Override
    public List<UserRewardPunishment> getUserRewardPunishment(String rid) throws Exception {
        return dao.find("from UserRewardPunishment where rid='" + rid + "'");
    }


    //家庭成员
    @SuppressWarnings("unchecked")
    @Override
    public List<UserFamily> getUserFamily(String rid) throws Exception {
        return dao.find("from UserFamily where rid='" + rid + "'");
    }

    @Override
    public List<DUserResume> getDUserResumes(String pid) throws Exception {
        @SuppressWarnings("unchecked")
        //List<DUserResume> resumes = dao.find("from DUserResume where proid='"+pid+"' and bdstate='已报到'");
                List<DUserResume> resumes = dao.find("from DUserResume where proid='" + pid + "' and nrid is null and fqstate='未放弃'");
        return resumes;
    }

    //保存简历
    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    @Transactional
    public void addResume(HRUsers user, List list, String uid) throws Exception {
        List<UserResume> resumes = dao.find("from UserResume where userid='" + uid + "'");
        List delList = new ArrayList();
        for (UserResume resume : resumes) {
            delList.add(resume);
            String rid = resume.getId();
            List<UserEducation> educations = dao.find("from UserEducation where rid='" + rid + "'");
            if (educations != null&&educations.size()>0) {
                delList.addAll(educations);
            }
            List<UserWorkExperience> workexperiences = dao.find("from UserWorkExperience where rid='" + rid + "'");
            if (workexperiences != null&&workexperiences.size()>0) {
                delList.addAll(workexperiences);
            }
            List<UserFamily> families = dao.find("from UserFamily where rid='" + rid + "'");
            if (families != null&&families.size()>0) {
                delList.addAll(families);
            }
            List<UserRewardPunishment> rewardpunishments = dao.find("from UserRewardPunishment where rid='" + rid + "'");
            if (rewardpunishments != null&&rewardpunishments.size()>0){
                delList.addAll(rewardpunishments);
            }

            List<UserTraining> trainings = dao.find("from UserTraining where rid='" + rid + "'");
            if (trainings != null&&trainings.size()>0) {
                delList.addAll(trainings);
            }
        }

        if (delList.size() > 0) {
            dao.deleteAll(delList);
        }
        dao.addAll(list);
        dao.update(user);
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean AllowSendResume(String pid, String userid) throws Exception {
        List<DUserResume> durs = dao.find("from DUserResume where proid='" + pid + "' and userid='" + userid + "'");
        if (durs.size() == 0) {
            return true;
        }
        int FQCount = 0;
        int WFQCount = 0;
        for (DUserResume dur : durs) {
            if (dur.getFqstate().equals("已放弃")) {
                FQCount++;
            }
            else {
                WFQCount++;
            }
        }
        if (FQCount <= 3 && WFQCount == 0) {
            return true;
        }
        return false;
    }

    @Override
    public synchronized String CreateResumeCode() {
        try {
            int cyear = Calendar.getInstance().get(Calendar.YEAR);
            int count = ((Long) dao.findUnique("select count(1) from UserResume where code like '" + cyear + "%'")).intValue();
            DecimalFormat df = new DecimalFormat("00000");
            return cyear + "-" + df.format(1 + count);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public DUserResume getDUserResume(String id) throws Exception {
        return (DUserResume) dao.findUnique("from DUserResume where id='" + id + "'");
    }

    @Override
    public UserResume getUserResumeByCard(String card) throws Exception {
        return (UserResume) dao.findUnique("from UserResume where cardno='" + card + "'");
    }

    //教育背景
    @SuppressWarnings("unchecked")
    @Override
    public List<DUserEducation> getDUserEducation(String rid) throws Exception {
        return dao.find("from DUserEducation where rid='" + rid + "' order by starttime desc");
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<UserEducation> getUserEducationZK(String rid) throws Exception {
        return dao.find("from UserEducation where rid='" + rid + "' order by endtime desc,educationcode asc");
    }

    //工作经验
    @SuppressWarnings("unchecked")
    @Override
    public List<DUserWorkExperience> getDUserWorkExperience(String rid) throws Exception {
        return dao.find("from DUserWorkExperience where rid='" + rid + "' order by starttime desc");
    }

    //培训经历
    @SuppressWarnings("unchecked")
    @Override
    public List<DUserTraining> getDUserTraining(String rid) throws Exception {
        return dao.find("from DUserTraining where rid='" + rid + "'");
    }

    //获奖信息
    @SuppressWarnings("unchecked")
    @Override
    public List<DUserRewardPunishment> getDUserRewardPunishment(String rid) throws Exception {
        return dao.find("from DUserRewardPunishment where rid='" + rid + "'");
    }

    //家庭成员
    @SuppressWarnings("unchecked")
    @Override
    public List<DUserFamily> getDUserFamily(String rid) throws Exception {
        return dao.find("from DUserFamily where rid='" + rid + "'");
    }

    @Override
    public Object findDUserByPlanJob(String pid, String jid, String userid) throws Exception {
        Map<String,Object> params=new HashMap<>();
        params.put("proid",pid);
        params.put("jid",jid);
        params.put("userid",userid);
        params.put("fqstate","未放弃");
        return dao.findUnique("select count(1) from DUserResume  where userid=:userid and jid=:jid and proid=:proid and fqstate=:fqstate",params);
    }

    @Override
    public List<UserMajor> getMajors(String rid) throws Exception {
        return dao.find("from UserMajor where rid='" + rid + "' order by postgettime desc");
    }

}
